package com.hs.carrental.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hs.carrental.common.Result;
import com.hs.carrental.dto.store.StoreDeleteDto;
import com.hs.carrental.dto.store.StorePageDto;
import com.hs.carrental.dto.store.StoreSaveDto;
import com.hs.carrental.service.IStoreService;
import com.hs.carrental.util.Helper;
import com.hs.carrental.vo.store.StorePageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyy
 * @since 2021-03-17
 */
@RestController
@RequestMapping("/carrental/store")
public class StoreController {

    @Autowired
    private IStoreService storeService;

    /**
     * 获取门店下拉
     * @return
     */
    @GetMapping("/getStoreEnum")
    public Result getStoreEnum(){
        return Result.ok(storeService.getStoreEnum());
    }

    /**
     * 门店新增 编辑
     * @param storeSaveDto
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody StoreSaveDto storeSaveDto){
        if (storeService.save(storeSaveDto)){
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * 删除
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody StoreDeleteDto storeDeleteDto){
        if(Helper.isEmpty(storeService.getById(storeDeleteDto.getId()))){
            return Result.failed("门店不存在");
        }
        if (storeService.delete(storeDeleteDto)){
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * 分頁查詢門店信息
     * @return
     */
    @PostMapping("/getPage")
    public Result<IPage<StorePageVo>> getPage(@RequestBody StorePageDto storePageDto){
        return Result.ok(storeService.getPage(storePageDto));
    }
}
